package com.example.salesservice.web;

import com.example.salesservice.entity.*;
import com.example.salesservice.service.SalesService;
import com.example.salesservice.utils.LayuiJson;
import com.example.salesservice.utils.ResponseState;
import com.example.salesservice.utils.ResultObj;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/sales")
public class SalesController {
    @Autowired
    private SalesService salesService;
    //注入JSON转换类
    @Autowired
    private LayuiJson layuiJson;

    /**
     * 加载获取所有销售信息(或根据条件手动分页查询)
     */
    @RequestMapping("loadAllSales")
    @ResponseBody
    public LayuiJson<Sales> loadAllSales(Integer customerid, Integer goodsid, Integer page, Integer limit){
        page = page == null ? 1 : page;
        limit = limit == null ? 5 : limit;
        //根据前端发送页码数据设置参数起始查询条件（默认为0）
        int index =(page - 1) * limit;
        //调用service 查询
        List<Sales> data = salesService.select(customerid, goodsid, index, limit);
        log.debug("data={}",data);
        //设置返回JSON
        layuiJson.setCode(0);
        layuiJson.setCount(1000);
        layuiJson.setData(data);

        return layuiJson;
    }

    /**
     * 添加商品销售信息(前端使用form表单发送)
     * @param salesVo
     * @return 新增操作结果
     */
    @RequestMapping("addSales")
    @ResponseBody
    public ResultObj addSales(Integer customerid, String paytype, Integer number,
                              String remark, Double saleprice, Integer goodsid,HttpSession session){
        try {
            //获取当前user信息
            //String operateperson = (String) session.getAttribute("username");
            String operateperson = "pzh";
            //获取当前时间date对象
            Date salestime=new Date();
            salesService.insert(customerid, paytype, salestime, operateperson, number, remark, saleprice, goodsid);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     *  根据前端录入更新供货商信息添加商品（前端使用form 传送信息）
     * @return 更新结果
     */
    @RequestMapping("/updateSales")
    @ResponseBody
    public ResultObj updateSales(Integer id,Integer customerid, String paytype, Integer number,
                                 String remark, Double saleprice, Integer goodsid,HttpSession session){
        try {
            //获取当前user信息
            //String operateperson = (String) session.getAttribute("username");
            String operateperson = "pzh";
            //获取当前时间date对象
            Date salestime=new Date();
            salesService.update(id, customerid, paytype, salestime, operateperson, number, remark, saleprice, goodsid);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除商品销售信息
     * @param id
     * @return
     */
    @RequestMapping("deleteSales")
    @ResponseBody
    public ResultObj deleteSales(Integer id){
        try {
            salesService.delete(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
